<?php
class Asig_usuController extends Controller
{
    var $widKey = false;
    public function actionCheck_data()
    {

    }
    public function actionSave_data()
    {
        $sql="select * from tbl_asignacion_usuarios au where au.asigu_mun=".$_GET['asigu_mun']." and au.asigu_usu=".$_GET['asigu_usu'];
        $consulta = Yii::app()->db->createCommand($sql)->queryAll();
        if(count($consulta)==0){
            $model = new tbl_asignacion_usuarios();
            $transaction = Yii::app()->db->beginTransaction();
            try{
                $model->attributes = $_GET;
                $this->widKey = $model->save();
                if($this->widKey){
                    $transaction->commit();
                    echo $model->primaryKey;
                }
            }catch(Exception $e){
                $this->widKey = false;
            }
            if(!$this->widKey){
                $transaction->rollback();
                echo "Error system controller 'Asig_usu' in the action -> Add.";
            }
        }
        else
            echo true;
    }

    public function actionDelete_data()
    {
        $transaction = Yii::app()->db->beginTransaction();
        try{
            $sql="delete from tbl_asignacion_usuarios where asigu_id=".$_GET['asigu_id'];
            $model = Yii::app()->db->createCommand($sql)->execute();
            if($model){
                $transaction->commit();
                echo "Eliminado";
            }
        }catch(Exception $e){
            $transaction->rollback();
            echo "Error system controller 'Asig_usu' in the action -> Delete.";
        }
        //$transaction = Yii::app()->db->beginTransaction();
    }
	//grid de listado de asignacion de usuarios a municipios y sus respectivas boletas
    public function actionasigUMBGrid_data()
    {
        $sql=  "select a.asig_bole,a.asig_mun,m.mun_detalle,b.bol_codigosea,a.asig_fechaini,a.asig_fechafin,a.asig_observacion,u.usu_id,CONCAT(u.usu_paterno,' ',u.usu_materno,' ',u.usu_nombres)as Nombre
                from tbl_asignacion  a,tbl_municipio  m ,tbl_boleta b,tbl_asignacion_usuarios au,
		             tbl_usuarios u
                where a.asig_mun=m.mun_id and a.asig_bole=b.bol_id and au.asigu_mun=m.mun_id and
			          au.asigu_usu=u.usu_id and u.usu_id='".$_GET['usu_id']."' order by a.asig_bole asc ";
        $model = Yii::app()->db->createCommand($sql)->queryAll();
        $data = Array();
        $cont = 0;
        foreach($model as $value){
            $cont++;
            $data[] = array('id'=>$cont,'data'=>array(
                $value['asig_bole'],
                $value['asig_mun'],
                $value['mun_detalle'],
                $value['bol_codigosea'],
                $value['asig_fechaini'],
                $value['asig_fechafin'],
                $value['asig_observacion'],
                //"Parte ".$value['bpa_numparte'].": ",
                $value['usu_id'],
                $value['Nombre']
            ));
        }
        $allData = array('rows'=>$data);
        echo 'data = '.CJSON::encode($allData);
    }
}